package demo.practice.binary_tree;

import demo.practice.TreeNode;

import java.util.*;

public class Offer32_1 {

    public static void main(String[] args) {

    }

    public int[] levelOrder(TreeNode root) {

        if(root==null)
            return new int[0];
        //Stack<TreeNode> stack = new Stack<>();
        Queue<TreeNode> queue= new LinkedList<>();

        queue.add(root);
        List<Integer> list = new ArrayList<>();
        while (queue.size() > 0) {
            List<TreeNode> lc = new ArrayList<>();
            while (queue.size() > 0) {
                TreeNode pop = queue.poll();
                list.add(pop.val);
                if (pop.left != null)
                    lc.add(pop.left);

                if (pop.right != null)
                    lc.add(pop.right);
            }
            queue.addAll(lc);

        }
        int[] res = new int[list.size()];
        int count = 0;
        for (Integer i : list) {
            res[count++] = i;
        }
        return res;
    }
}
